<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsPurchaseSummary extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['i.purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['i.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $supplyer_code = $this->request->post('supplyer_code');
            if(!empty($supplyer_code)){
                $where['i.supplyer_code'] = $supplyer_code;
            }
            $goods_code = $this->request->post('goods_code');
            if(!empty($goods_code)){
                $where['i.goods_code'] = $goods_code;
            }
            $keyword = $this->request->post('keyword');
            if(!empty($keyword)){
                $where['i.supplyer_code|i.goods_code'] = $keyword;
            }

            $field = empty(input('field'))?'goods_code':trim(input('field'));
            $order = empty(input('sort'))?'DESC':trim(input('sort'));

            $price_decimal = \think\Db::name('kuyou_sets')->where(['name'=>'price_decimal'])->value('value');
            $number_decimal = \think\Db::name('kuyou_sets')->where(['name'=>'number_decimal'])->value('value');

            $list = [];
            $lists = Db::name('kuyou_goods_purchase_summary')->alias('i')
                ->join('kuyou_goods g','g.goods_code=i.goods_code')
                ->join('kuyou_goods_type gt','gt.id=g.goods_type_id')
                ->join('kuyou_stores s','s.id=i.stores_id')
                ->join('kuyou_unit u','u.id=i.goods_unit')
                ->where($where)
                ->order(['i.'.$field=>$order])
                ->group('i.goods_code,i.stores_id')
                ->field('sum(i.tax_money) as tax_money,sum(i.money_tax_price) as money_tax_price,i.goods_code,i.stores_id,sum(i.basic_nmber) as basic_nmber,sum(number) as i_number,unit_price,sum(all_money) as all_money,i.default_unit,i.goods_unit,i.purchase_money,
                g.goods_code,g.goods_name,g.goods_specification_id as goods_spec,g.more_unit,g.id as goods_id,
                gt.supplier_type_name,
                s.name as s_name,u.name as u_name')
                ->select();
            if(!empty($lists)){
                foreach ($lists as $lists_k=>$lists_v){
                    $lists_v['i_number'] = $lists_v['basic_nmber'];

                    $lists_v['unit_conversion'] = '';
                    if($lists_v['more_unit']==1){
                        $goodsUnit = (new \app\api\model\kuyou\applet\Goods())->getGoodsUnit($lists_v['goods_id']);
                        $unit_data = isset($goodsUnit['unit_data'])?$goodsUnit['unit_data']:[];
                        if(!empty($unit_data)){
                            $conversion = (new Commons())->getConversion($lists_v['i_number'],$unit_data);
                            if(isset($conversion['basic_nmber']) && isset($conversion['unit_conversion'])){
                                $lists_v['basic_nmber'] = $conversion['basic_nmber'];
                                $lists_v['unit_conversion'] = $conversion['unit_conversion'];
                            }else{
                                $lists_v['basic_nmber'] = '';
                                $lists_v['unit_conversion'] = '';
                            }
                        }
                    }

                    if(!empty($list)){
                        foreach ($list as $list_k=>$list_v){
                            if($list_v['goods_code']==$lists_v['goods_code'] && $list_v['stores_id']==$lists_v['stores_id']){
                                $list[$list_k]['i_number'] += $lists_v['i_number'];
                                $list[$list_k]['all_money'] += $lists_v['all_money'];
                                $list[$list_k]['purchase_money'] += $lists_v['purchase_money'];
                                $list[$list_k]['unit_price'] = round(($list[$list_k]['all_money']/$list[$list_k]['i_number']),$price_decimal);
                                $status = true;
                                $list[$list_k]['basic_nmber'] = $lists_v['basic_nmber'];
                                $list[$list_k]['unit_conversion'] = $lists_v['unit_conversion'];
                            }
                        }
                    }
                    if(!isset($status)){
                        $list[] = $lists_v;
                    }
                }
            }
            $all_money = 0;$i_number = 0;$purchase_money = 0;$tax_money = 0;$money_tax_price = 0;
            foreach ($list as $list_k=>$list_v){
                $all_money += $list_v['all_money'];
                $i_number += $list_v['i_number'];
                $purchase_money += $list_v['purchase_money'];
                $tax_money += $list_v['tax_money'];
                $money_tax_price += $list_v['money_tax_price'];
                $list[$list_k]['all_money'] = round($list_v['all_money'],$price_decimal);
                if($list_v['i_number']!=0){
                    $list[$list_k]['unit_price'] = round($list_v['all_money']/$list_v['i_number'],$price_decimal);
                }else{
                    $list[$list_k]['unit_price'] = 0;
                }
                $list[$list_k]['tax_money'] = round($list_v['tax_money'],$price_decimal);
                $list[$list_k]['money_tax_price'] = round($list_v['money_tax_price'],$price_decimal);
            }
            if($i_number>0){
                $unit_price = round($all_money/$i_number,$price_decimal);
            }else{
                $unit_price = 0;
            }
            $i_number = round($i_number,$number_decimal);
            $all_money = round($all_money,$price_decimal);
            $purchase_money = round($purchase_money,$price_decimal);
            $tax_money = round($tax_money,$price_decimal);
            $money_tax_price = round($money_tax_price,$price_decimal);
            $count = [
                'i_number'=>$i_number,
                'unit_price'=>$unit_price,
                'purchase_money'=>$purchase_money,
                'all_money'=>$all_money,
                'tax_money'=>$tax_money,
                'money_tax_price'=>$money_tax_price,
            ];

            $result = array("count" => sizeof($list), "list" => $list,'sum'=>$count);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
